// index.js
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'

Page({
  data: {
    motto: 'Hello World',
    userInfo: {
      avatarUrl: defaultAvatarUrl,
      nickName: '',
    },
    hasUserInfo: false,
    canIUseGetUserProfile: wx.canIUse('getUserProfile'),
    canIUseNicknameComp: wx.canIUse('input.type.nickname'),
    qrCodeContent: '', // 存储二维码内容
    barcodeContent: '', // 存储条形码内容
    latitude: '',
    longitude: '',
    address: '',
    hasLocation: false
  },
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onChooseAvatar(e) {
    const { avatarUrl } = e.detail
    const { nickName } = this.data.userInfo
    this.setData({
      "userInfo.avatarUrl": avatarUrl,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  onInputChange(e) {
    const nickName = e.detail.value
    const { avatarUrl } = this.data.userInfo
    this.setData({
      "userInfo.nickName": nickName,
      hasUserInfo: nickName && avatarUrl && avatarUrl !== defaultAvatarUrl,
    })
  },
  getUserProfile(e) {
    // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认，开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
    wx.getUserProfile({
      desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
      success: (res) => {
        console.log(res)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    })
  },
  // 扫描二维码
  scanQRCode() {
    wx.scanCode({
      onlyFromCamera: true,
      scanType: ['qrCode'],
      success: (res) => {
        this.setData({
          qrCodeContent: res.result
        })
      },
      fail: (err) => {
        wx.showToast({
          title: '扫码失败',
          icon: 'error'
        })
      }
    })
  },

  // 扫描条形码
  scanBarcode() {
    wx.scanCode({
      onlyFromCamera: true,
      scanType: ['barCode'],
      success: (res) => {
        this.setData({
          barcodeContent: res.result
        })
      },
      fail: (err) => {
        wx.showToast({
          title: '扫码失败',
          icon: 'error'
        })
      }
    })
  },
  // 获取位置信息
  getLocation() {
    wx.authorize({
      scope: 'scope.userLocation',
      success: () => {
        this.getLocationInfo()
      },
      fail: () => {
        wx.showModal({
          title: '位置权限',
          content: '需要获取您的地理位置，请确认授权',
          success: (res) => {
            if (res.confirm) {
              wx.openSetting({
                success: (res) => {
                  if (res.authSetting['scope.userLocation']) {
                    this.getLocationInfo()
                  }
                }
              })
            }
          }
        })
      }
    })
  },

  // 获取具体位置信息
  getLocationInfo() {
    wx.showLoading({ title: '获取位置中...' })
    wx.getLocation({
      type: 'gcj02',
      success: (res) => {
        const { latitude, longitude } = res
        this.setData({ latitude, longitude })
        
        // 使用微信小程序的地址解析接口
        wx.chooseLocation({
          latitude,
          longitude,
          success: (res) => {
            this.setData({
              address: res.address,
              hasLocation: true
            })
          },
          complete: () => {
            wx.hideLoading()
          }
        })
      },
      fail: () => {
        wx.hideLoading()
        wx.showToast({
          title: '获取位置失败',
          icon: 'error'
        })
      }
    })
  }
})
